// Package requests 处理请求数据和表单验证逻辑
package requests

import (
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"

	"github.com/runningwater/gohub/app/requests/validators"
)

type SignupPhoneExistRequest struct {
	Phone string `json:"phone,omitempty" valid:"phone"`
}
type SignupEmailExistRequest struct {
	Email string `json:"email,omitempty" valid:"email"`
}

func ValidateSignupPhoneExist(data any, c *gin.Context) map[string][]string {

	// 自定义验证规则
	rules := govalidator.MapData{
		"phone": []string{"required", "digits:11"},
	}

	// 自定义错误信息
	messages := govalidator.MapData{
		"phone": []string{
			"required:手机号不能为空",
			"digits:手机号必须是11位数字",
		},
	}

	// 执行验证
	return validate(data, rules, messages)
}

func ValidateSignupEmailExist(data any, c *gin.Context) map[string][]string {

	// 自定义验证规则
	rules := govalidator.MapData{
		"email": []string{"required", "min:4", "max:30", "email"},
	}

	// 自定义错误信息
	messages := govalidator.MapData{
		"email": []string{
			"required:Email 不能为空",
			"min:Email 长度需大于 4",
			"max:Email 长度需小于 30",
			"email:Email 格式不正确，请提供有效的邮箱地址",
		},
	}
	// 执行验证
	return validate(data, rules, messages)
}

// SignupUsingPhoneRequest 用于手机注册的请求结构体
// 该结构体包含了手机号码、验证码、姓名、密码和确认密码等字段
type SignupUsingPhoneRequest struct {
	Phone           string `json:"phone,omitempty" valid:"phone"`
	VerifyCode      string `json:"verify_code,omitempty" valid:"verify_code"`
	Name            string `valid:"name" json:"name,omitempty"`
	Password        string `valid:"password" json:"password,omitempty"`
	PasswordConfirm string `valid:"password_confirm" json:"password_confirm,omitempty"`
}

// SignupUsingPhone 验证手机号码注册表单
func SignupUsingPhone(data any, c *gin.Context) map[string][]string {
	// 自定义验证规则
	rules := govalidator.MapData{
		"phone":            []string{"required", "digits:11", "not_exists:users,phone"},
		"verify_code":      []string{"required", "digits:6"},
		"name":             []string{"required", "alpha_num", "between:3,20", "not_exists:users,name"},
		"password":         []string{"required", "min:6"},
		"password_confirm": []string{"required"},
	}

	// 自定义错误信息
	messages := govalidator.MapData{
		"phone": []string{
			"required:手机号不能为空",
			"digits:手机号必须是 11 位数字",
		},
		"verify_code": []string{
			"required:验证码不能为空",
			"digits:验证码必须是 6 位数字",
		},
		"name": []string{
			"required:姓名不能为空",
			"alpha_num:姓名格式错误，只允许数字和英文",
			"between:姓名长度需在 3~20 之间",
		},
		"password": []string{
			"required:密码不能为空",
			"min:密码长度需大于 6",
		},
		"password_confirm": []string{
			"required:确认密码框不能为空",
		},
	}

	errs := validate(data, rules, messages)

	// 检查密码和确认密码是否匹配
	_data := data.(*SignupUsingPhoneRequest)
	errs = validators.ValidatePasswordConfirm(_data.Password, _data.PasswordConfirm, errs)
	errs = validators.ValidateVerifyCode(_data.Phone, _data.VerifyCode, errs)

	return errs
}

// SignupUsingEmailRequest 用于邮箱注册的请求结构体
// !该结构体包含了邮箱、验证码、姓名、密码和确认密码等字段
type SignupUsingEmailRequest struct {
	Email           string `json:"email,omitempty" valid:"email"`
	VerifyCode      string `json:"verify_code,omitempty" valid:"verify_code"`
	Name            string `valid:"name" json:"name"`
	Password        string `valid:"password" json:"password,omitempty"`
	PasswordConfirm string `valid:"password_confirm" json:"password_confirm,omitempty"`
}

// SignupUsingEmail 验证邮箱注册表单
func SignupUsingEmail(data any, c *gin.Context) map[string][]string {

	rules := govalidator.MapData{
		"email":            []string{"required", "min:4", "max:30", "email", "not_exists:users,email"}, // 邮箱验证规则
		"verify_code":      []string{"required", "digits:6"},                                           // 验证码验证规则
		"name":             []string{"required", "alpha_num", "between:3,20", "not_exists:users,name"}, // 姓名验证规则
		"password":         []string{"required", "min:6"},                                              // 密码验证规则
		"password_confirm": []string{"required"},                                                       // 确认密码验证规则
	}
	// 自定义错误信息
	messages := govalidator.MapData{
		"email": []string{
			"required:Email 不能为空",
			"min:Email 长度需大于 4",
			"max:Email 长度需小于 30",
			"email:Email 格式不正确，请提供有效的邮箱地址",
			"not_exists:邮箱已被注册",
		},
		"verify_code": []string{
			"required:验证码不能为空",
			"digits:验证码必须是 6 位数字",
		},
		"name": []string{
			"required:姓名不能为空",
			"alpha_num:姓名格式错误，只允许数字和英文",
			"between:姓名长度需在 3~20 之间",
		},
		"password": []string{
			"required:密码不能为空",
			"min:密码长度需大于 6",
		},
		"password_confirm": []string{
			"required:确认密码框不能为空",
		},
	}

	errs := validate(data, rules, messages)
	// 检查密码和确认密码是否匹配
	_data := data.(*SignupUsingEmailRequest)
	errs = validators.ValidatePasswordConfirm(_data.Password, _data.PasswordConfirm, errs)
	errs = validators.ValidateVerifyCode(_data.Email, _data.VerifyCode, errs)

	return errs
}
